# Define your item pipelines here
#
# Don't forget to add your pipeline to the ITEM_PIPELINES setting
# See: https://docs.scrapy.org/en/latest/topics/item-pipeline.html


# useful for handling different item types with a single interface
import json
import os

from itemadapter import ItemAdapter
from pymongo import MongoClient


class ClearData:
    def process_item(self, item, spider):
        if item.get('type') == 'comment':
            comment_json = json.loads(item.get('comment_info'))
            parent_comment_list = comment_json.get('data').get('commentList')

            news_comment_list = []
            if parent_comment_list:
                for parent_comment in parent_comment_list:
                    comment_dict = {}
                    nick_name = parent_comment.get('user').get('nick')
                    content = parent_comment.get('content')

                    child_list = []
                    child_comment_list = parent_comment.get('childs')
                    if child_comment_list:
                        for child in child_comment_list:
                            c_dict = {}
                            c_nick_name = child.get('user').get('nick')
                            c_content = child.get('content')

                            c_dict['c_nick_name'] = c_nick_name
                            c_dict['c_content'] = c_content
                            child_list.append(c_dict)

                    comment_dict['nick_name'] = nick_name
                    comment_dict['content'] = content
                    comment_dict['child_list'] = child_list
                    news_comment_list.append(comment_dict)
            item['comment_info'] = news_comment_list
            return item
        else:
            return item


class ChoutiPipeline:
    def __init__(self):
        self.client = MongoClient('localhost', 27017)

        self.download_img_path = os.getcwd() + '/images/'
        if not os.path.exists(self.download_img_path):
            os.mkdir(self.download_img_path)

    def process_item(self, item, spider):
        type_ = item.get('type')
        item.pop('type')
        if type_ == 'info':
            collection = self.client['ChouTi']['chouti_info']
            collection.insert_one(item)
        elif type_ == 'img':
            with open(self.download_img_path + item.get('title') + '.jpeg', 'wb') as f1:
                f1.write(item.get('content'))
        elif type_ == 'comment':
            collection = self.client['ChouTi']['chouti_comment']
            collection.insert_one(item)
